<?php
namespace app\index\model;

use think\Model;
use think\Db;

class PublishedSeismic extends Model {

    public function insertEqInfo($data) {
        $result = Db::table('published_seismic')->insert($data);
        return $result;
    }

    public function wechatPush($eqUnique) {
        $result = Db::table('published_seismic')
                    ->field("sec_abbreviation as district, eq_time as time, longitude as lon, 
                             latitude as lat, magnitude as magn, Intensity as intensity, depth as depth")
                    ->where("ID = '" . $eqUnique . "'")
                    ->select();
        return $result;
    }

    public function getWeaData($eqUnique) {
        $map = [ 'ID' =>  ['=', $eqUnique] ];
        $result = Db::table('published_seismic')
                    ->field('eq_name, province, fir_abbreviation, climate, weather')
                    ->where($map)
                    ->select();      
        return $result[0];
    }

    public function getEconData($eqUnique) {
        $result = Db::table('published_seismic')
                    ->field('eq_name, ROUND(GDP/10000, 2) as GDP, ROUND(fir_industry/10000, 2) as FST_INDUSTRY,
                             ROUND(sec_industry/10000, 2) as SND_INDUSTRY, ROUND(thi_industry/10000, 2) as TRD_INDUSTRY,economic')
                    ->where("ID = '".$eqUnique."'")
                    ->select();
        return $result;
    }
    
    public function getEnvirData($eqUnique) {
        $result = Db::table('published_seismic')
                    ->field('eq_name, min_altitude, ave_altitude, max_altitude, topographic, environment')
                    ->where("ID = '".$eqUnique."'")
                    ->select();
        return $result;
    }

    public function getPeoData($eqUnique) {
        $result = Db::table('published_seismic')
                    ->field('eq_name, ROUND((pop_countryside+pop_town)/10000, 2) as POP_ALL, 
                             ROUND(pop_countryside/10000, 2) as POP_COUNTRYSIDE, ROUND(pop_town/10000, 2) as POP_TOWN,
                             urban_ave_income, village_ave_income, village_num, town_num, pop_detail, people')
                    ->where("ID = '".$eqUnique."'")
                    ->select();
        return $result;
    }

    public function getEpicenter($eqUnique) {
        $result = Db::table('published_seismic')
                    ->field("sec_abbreviation as district, eq_time as time, 
                             longitude, latitude, magnitude, intensity, depth")
                    ->where("ID = '".$eqUnique."'")
                    ->select();          
        Db::table('published_seismic')->where('ID',$eqUnique)->update(['eqcenterInfo'=> $result[0]['district']]);
        return $result;
    }

    public function getNearProvince($eqUnique) {
        $VirulTable = Db::table('published_seismic')
                        ->field('province, x_longitude, x_latitude, magnitude, eq_time')
                        ->where("ID = '".$eqUnique."'")
                        ->buildSql();
        $result = Db::field("ps.province, CONCAT_WS('，', ps.neighbor, ps.province) as neighbor,
                                p.x_longitude, p.x_latitude, p.magnitude, p.eq_time")
                        ->table('province as ps, '.$VirulTable.' as p')
                        ->where('ps.province = p.province')
                        ->select();
        return $result;
    }

    // 五个字段的资源信息
    public function getSoucreData($eqUnique){
        $result =Db::table('published_seismic')
                ->field('weather as city_weather, 
                         people as city_population,
                         economic as city_economics,
                         environment as city_environment,
                         eqcenterInfo as city_name')
                ->where("ID = '".$eqUnique."'")
                ->select();
        return $result[0];
    }

    public function getCode($eqUnique) {
        $result =Db::table('published_seismic')
                ->field('eq_time as time')
                ->where("ID = '".$eqUnique."'")
                ->select();
        return $result[0]['time'];
    }

}